package com.juyu.miliao.api.service;

import com.juyu.miliao.api.dao.redis.UserRedisDao;
import com.juyu.miliao.api.domain.NewBieGuide;
import com.juyu.miliao.api.rabbitMQ.BillDetailBeansMq;
import com.juyu.miliao.api.service.cache.NewBieGuideCacheService;
import com.juyu.miliao.common.builder.BillDetailBeansMqMessageBuilder;
import com.juyu.miliao.common.constant.BeansType;
import com.juyu.miliao.common.domain.dto.BillDetailBeansDTO;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: 卜加超
 * @Date: 2019/5/21 10:32
 * @Description:
 */
@Service
public class NewBieGuideService {
    private static final Logger NewBieGuideServiceLogger = LoggerFactory.getLogger(MessageSettingsService.class);
    @Resource
    private NewBieGuideCacheService newBieGuideCacheService;
    @Resource
    private UserWalletService userWalletService;
    @Resource
    private UserRedisDao userRedisDao;
    @Autowired
    private BillDetailBeansMq billDetailBeansMq;

    public List<NewBieGuide> getNewBieGuideList(int type) {
       List<NewBieGuide> list = newBieGuideCacheService.getList();
        List<NewBieGuide> showList = new ArrayList<>();
        for (NewBieGuide newBie: list ) {
            if(newBie.getGuideType() == type){
                showList.add(newBie);
            }
        }
       return showList;
    }

    public Map<String,Object> giveUserBeans(long userId, long beans) {
        Map<String,Object> map = new HashMap<>();
        if(!userRedisDao.isGiveUserBeans(userId)){
            int i = userWalletService.addBeans(userId, beans);
            if(i>0){
                BillDetailBeansMqMessageBuilder builder = new BillDetailBeansMqMessageBuilder();
                BillDetailBeansDTO billDetailBeansDTO = builder.userId(userId).beans(beans)
                        .beansType(BeansType.NOVICE_GUIDANCE_AWARD_BEANS)
                        .createDate(LocalDate.now(ZoneOffset.of("+8")))
                        .createTime(LocalDateTime.now(ZoneOffset.of("+8"))).build();
                billDetailBeansMq.getBillDetailBeansMq(billDetailBeansDTO);
                userRedisDao.saveGiveUserBeans(userId,beans);
                map.put("code",1);
                map.put("message","领取成功");
            }

        }else {
            map.put("code",2);
            map.put("message","您已领取过");
        }
        return map;
    }

}
